package consumer5;

import java.util.Queue;

public class Producer implements Runnable {
	int num = 0;
	private final Queue<Integer> q;

	public Producer(Queue<Integer> q) {
		this.q = q;
	}

	@Override
	public void run() {
		try {
			while (num < 20) {
				synchronized (q) {
					if (q.size() > 10)
						q.wait(10000);
					q.add(produce());
					q.notifyAll();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	Integer produce() {
		return ++num;
	}
}
